const query = require('./query')

const queryGoodsPriceById = async (goods_id) => {
  const sql = 'select DISTINCT goods_price from goodsInfo where goodsInfo_status = 1 and goods_id = ? order by goods_price asc'
  try {
    const result = await query(sql, goods_id)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}

const queryGoodsDiscountByIdAndPrice = async (goods_id, goods_price) => {
  const sql = 'select DISTINCT goods_discount from goodsInfo where goodsInfo_status = 1 and goods_id = ? and goods_price = ?'
  try {
    const result = await query(sql, [goods_id, goods_price])
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}

const queryGoodsImageIdByGoodsId = async (goods_id) => {
  const sql = `select DISTINCT goods_id,goods_image_id
  from goodsInfo 
  where goodsInfo_status = 1 
  and cPhone_version_id is not null
  and goods_id = ?
  order by goods_image_id ASC`
  try {
    const result = await query(sql, goods_id)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}

const queryGoodsByImageId = async (image_id) => {
  const sql = `select a.goodsInfo_id,a.goods_id,d.goods_name,a.goods_price,a.goods_discount,b.cphone_version_name,c.goods_image_path
  from goodsInfo a
  join cphone_version b
  join goods_image c
  join goods d
  where a.goodsInfo_status = 1 
  and a.goods_image_id = ?
  and a.cphone_version_id = b.cphone_version_id
  and a.goods_image_id = c.goods_image_id
  and a.goods_id = d.goods_id
  `
  try {
    const result = await query(sql, image_id)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
const queryGoodsInfoByGoodsId = async (goods_id) => {
  const sql = `select 
  a.goodsInfo_id,
  a.goods_id,
  a.goods_price,
  a.goods_discount,
  c.goods_color_name as goods_color,
  d.cPhone_version_name as goods_version,
  e.goods_desc,
  e.goods_name,
  e.goods_number
  from goodsInfo a
  join goods_image b
  join goods_color c
  join cPhone_version d
  join goods e
  where a.goodsInfo_status = 1
  and a.goods_id = ?
  and a.goods_image_id = b.goods_image_id
  and b.goods_color_id = c.goods_color_id
  and a.cPhone_version_id = d.cPhone_version_id
  and a.goods_id = e.goods_id
  order by a.goodsInfo_id asc
  `
  try {
    const result = await query(sql, goods_id)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
const queryGoodsInfoByGoodsInfoId = async (goodsInfo_id) => {
  const sql = `select a.goodsInfo_id,d.goods_id,d.goods_name,b.cPhone_version_name,c.goods_image_path,a.goods_price,e.goods_color_name
  from goodsInfo a
  join cPhone_version b
  join goods_image c
  join goods d
  join goods_color e
  where a.goodsInfo_id in (${(Object.values(goodsInfo_id))})
  and a.goodsInfo_status = 1
  and a.cPhone_version_id = b.cPhone_version_id
  and a.goods_image_id = c.goods_image_id
  and a.goods_id = d.goods_id
  and c.goods_color_id = e.goods_color_id`
  try {
    const result = await query(sql)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
module.exports = {
  queryGoodsPriceById,
  queryGoodsDiscountByIdAndPrice,
  queryGoodsImageIdByGoodsId,
  queryGoodsByImageId,
  queryGoodsInfoByGoodsId,
  queryGoodsInfoByGoodsInfoId
}